package com.ktjy.crm.thymeleaf.service.imp;

import com.ktjy.crm.thymeleaf.entity.SysUser;
import com.ktjy.crm.thymeleaf.mapper.UserMapper;
import com.ktjy.crm.thymeleaf.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;

//    @Override
//    public SysUser login(String usrName, String usrPassword) {
//        SysUser user = userMapper.getUserByUsrNameWithRoleName(usrName);
//        if (user != null) {
//            // 判断密码是否正确（假设密码已加密存储）
//            if (user.getPassword().equals(usrPassword)) {
//                return user;
//            }
//        }
//        return null; // 密码错误或用户不存在
//    }
//
//    @Override
//    public int addUser(SysUser user) {
//        // 确保在插入之前进行必要的验证
//        if (userMapper.selectByUsrName(user.getUsrName()) != null) {
//            throw new IllegalArgumentException("Username already exists");
//        }
//        return userMapper.insert(user);
//    }

    @Override
    public int deleteUser(Long usrId) {
        return userMapper.deleteById(usrId);
    }

    @Override
    public int updateUser(SysUser user) {
        // 确保在更新之前进行必要的验证
        if (userMapper.selectById(user.getId()) == null) {
            throw new IllegalArgumentException("User not found");
        }
        return userMapper.updateById(user);
    }

    @Override
    public SysUser getUser(Long usrId) {
        return userMapper.selectById(usrId);
    }

    @Override
    public List<SysUser> findAllUsers() {
        return userMapper.getUserListWithRoleName();
    }
}
